home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- distort.c
- Drew Olbrich, 1992
-
- This program demonstrates how texture mapping can be
- used for interactive image distortion effects.
-
- In this demo, an arbitrarily-sized image is mapped onto
- a large array of texture mapped polygons. The pop-up menu
- can be used to choose between different kinds of distortion.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <gl.h>
- #include <device.h>
-
- #include "imagedata.h"
- #include "defs.h"
-
- static EFFECT *effect = &DEFAULT_EFFECT;
-
- static long menu; /* pop up menu handle */
-
- /*
- Open and initialize a GL window.
- */
-
- void display_init()
- {
- foreground();
- prefsize(WIN_SIZE_X, WIN_SIZE_Y);
- winopen(WIN_TITLE);
- icontitle(ICON_TITLE);
-
- doublebuffer();
- RGBmode();
- ortho(-0.5, WIN_SIZE_X - 0.5, -0.5, WIN_SIZE_Y - 0.5, CLIP_NEAR, CLIP_FAR);
- gconfig();
-
- cpack(0x00000000);
- clear();
-
- cpack(0xFFFFFFFF);
- cmov2i((WIN_SIZE_X - strwidth(WAIT_MSG))/2,
- (WIN_SIZE_Y - getlwidth())/2);
- charstr(WAIT_MSG);
-
- swapbuffers();
-
- qdevice(REDRAW);
- qdevice(MOUSEX);
- qdevice(MOUSEY);
- qdevice(LEFTMOUSE);
- qdevice(RIGHTMOUSE);
- qdevice(ESCKEY);
- qdevice(WINSHUT);
- qdevice(WINQUIT);
- qdevice(WINCLOSE);
-
- menu = defpup("Distortion %t|Ripple|Rubber %l|Quit");
- }
-
- /*
- Load the image to distort, make a texture out of it,
- and bind it.
- */
-
- void image_init(char *fn)
- {
- unsigned long *buf;
- int width, height;
- static float tex_props[] = { TX_WRAP, TX_CLAMP, TX_NULL };
- static float tev_props[] = { TV_NULL };
-
- buf = longimagedata(fn, &width, &height);
- if (buf == NULL)
- {
- fprintf(stderr, "distort: Can't load image file \"%s\".\n", fn);
- exit(-1);
- }
-
- texdef2d(1, 4, width, height, buf, 0, tex_props);
- tevdef(1, 0, tev_props);
-
- texbind(TX_TEXTURE_0, 1);
- tevbind(TV_ENV0, 1);
-
- free(buf);
- }
-
- /*
- Chew on those nifty GL events for a while.
- */
-
- void event_loop()
- {
- int done;
- long dev;
- short val;
- long originx, originy;
- int mousex, mousey;
-
- getorigin(&originx, &originy);
-
- done = 0;
- while (!done)
- {
- effect->dynamics(mousex, mousey);
- effect->redraw();
-
- while (qtest())
- {
- dev = qread(&val);
- switch (dev)
- {
- case REDRAW :
- getorigin(&originx, &originy);
- effect->redraw();
- break;
- case LEFTMOUSE :
- effect->click(mousex, mousey, val);
- break;
- case RIGHTMOUSE :
- switch (dopup(menu))
- {
- case 1 :
- effect = &ripple;
- effect->init();
- break;
- case 2 :
- effect = &rubber;
- effect->init();
- break;
- case 3 :
- done = 1;
- break;
- }
- break;
- case MOUSEX :
- mousex = val - originx;
- break;
- case MOUSEY :
- mousey = val - originy;
- break;
- case WINQUIT :
- case WINSHUT :
- case WINCLOSE :
- case ESCKEY :
- done = 1;
- break;
- }
- }
- }
- }
-
- /*
- If a command line argument is provided, it is interpreted
- as the name of an alternate source image.
- */
-
- void main(int argc, char **argv)
- {
- if (!getgdesc(GD_TEXTURE))
- {
- char buf[256];
- FILE *file;
-
- file = fopen("/usr/sbin/inform", "r");
- if (file == NULL)
- fprintf(stderr, "%s: %s\n", argv[0], MSG_NO_TEXTURING);
- else
- {
- fclose(file);
- sprintf(buf, "/usr/sbin/inform %s", MSG_NO_TEXTURING);
- system(buf);
- }
-
- exit(-1);
- }
-
- display_init();
-
- effect->init();
-
- if (argc == 1)
- image_init(DEFAULT_IMAGE_FN);
- else
- image_init(argv[1]);
-
- event_loop();
- }
-